<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/17
 * Time: 14:32
 */

namespace app\index\middleware;

use app\index\business\RedisCache;
use app\libs\Predis;
use gmars\rbac\Rbac;
use think\Request;

class Auth
{
    protected $redis = null;
    protected $request = null;


    public function __construct()
    {
        $this->redis = new Predis();
        $this->request = Request::instance();

    }

    //登录检测
    public function beforeExecuteRoute()
    {
        $role = 'visitor';
        $controller = $this->request->controller();
        $action = $this->request->action();

        //获取token 并判断是否过期
        $token = $this->request->header('token');
        $tokenPassword = $this->request->header('tokenPassword');

        $token = str_replace('%3D', '=', $token);
        if (base64_decode($token)) {
            $token = base64_decode($token);
        }

        $redisCache = new RedisCache();

        //对此控制器方法是否有访问权限
        $resource = $controller . "::" . $action;
        $user = $redisCache->checkToken($token, $tokenPassword);
        if ($user['status'] == 1) {
            $role = "user";
//            //判断访问流量
//            $msg = '请求过快';
//            $key = 'qc:userNo:' . $user['data']['user_no'] . ':api:' . $resource;
//            $isset = $this->redis->get($key);
//            if ($isset) {
//                return $this->ajaxReturn([
//                    'status' => -4,
//                    'msg' => $msg,
//                    'data' => []
//                ]);
//            }
//            $this->redis->set($key, '1', 1);
            // 设置调度信息
            $this->request->dispatch(['user_no'=> $user['data']['user_no'],'token_password' =>$tokenPassword]);
        }

//        var_dump($resource);
//        var_dump($role);die();
        if ($this->allow($role, $resource)) {
            return true;
        }

        return $this->ajaxReturn([
            'status' => -2,
            'msg' => '需要登录',
            'data' => []
        ]);
    }

    public function allow($role = "visitor", $resource)
    {
        $acl = array(
            'visitor' => array(
                'User::login',
                'User::register',
                'User::setpassword',
                'User::newpassword',
                'User::tinywindow',
                'User::feedback',
                'Wx::login',
                'Mobile::sendsms',
                'Industry::industrycategory',
                'Industry::industrynews',
                'Industry::industrydetail',
                'TopButton::city',
                'TopButton::industrychain',
                'TopButton::industryclassify',
                'TopButton::policytype',
                'TopButton::projecttop',
                'TopButton::chainmorere',
                'TopButton::chaindropbox',
                'TopButton::chaincitymap',
                'TopButton::importantcity',
                'TopButton::common',
                'TopButton::chainrate',
                'TopButton::mistaketype',
                'TopButton::correctmistake',
                'Policy::policylibrarylist',
                'Policy::policylibrarydetail',
                'Policy::getcitypolicy',
                'Policy::citydiffpolicy',
                'Park::parklist',
                'Park::parkdetail',
                'Company::companylist',
                'Company::companydetail',
                'Company::companycommentlist',
                'Company::pushcompany',
                'Invest::investlist',
                'Invest::investdetail',
                'Invest::tanklist',
                'Invest::tankdetail',
                'CrmProject::indexnewslist',
                'CrmProject::indexnewsdetail',
                'CrmProject::indexprojectlist',
                'CrmProject::indexprojectdetail',
                'Test::test',
                'Test::test2',
            ),
            'user' => array(
                'User::login',
                'User::register',
                'User::setpassword',
                'User::newpassword',
                'User::collectbehavior',
                'User::cancelcollection',
                'User::uploads',
                'User::publish',
                'User::tinywindow',
                'User::feedback',
                'Mobile::sendsms',
                'Industry::industrycategory',
                'Industry::industrynews',
                'Industry::industrydetail',
                'TopButton::city',
                'TopButton::industrychain',
                'TopButton::industryclassify',
                'TopButton::policytype',
                'TopButton::projecttop',
                'TopButton::chainmorere',
                'TopButton::chaindropbox',
                'TopButton::chaincitymap',
                'TopButton::importantcity',
                'TopButton::common',
                'TopButton::chainrate',
                'TopButton::mistaketype',
                'TopButton::correctmistake',
                'Policy::policylibrarylist',
                'Policy::policylibrarydetail',
                'Policy::getcitypolicy',
                'Policy::citydiffpolicy',
                'Park::parklist',
                'Park::parkdetail',
                'Company::companylist',
                'Company::companydetail',
                'Company::companycommentlist',
                'Company::companycommentadd',
                'Company::pushcompany',
                'Invest::investlist',
                'Invest::investdetail',
                'Invest::tanklist',
                'Invest::tankdetail',
                'index::notFound',
                'CrmCollect::crmcollectlist',
                'CrmProject::addproject',
                'CrmProject::editproject',
                'CrmProject::detailproject',
                'CrmProject::logproject',
                'CrmProject::listproject',
                'CrmProject::monitorproject',
                'CrmProject::delproject',
                'CrmProject::crmdepartment',
                'CrmProject::allproject',
                'CrmProject::memberlist',
                'CrmProject::crmpushproject',
                'CrmProject::crmpushprojectdetail',
                'CrmProject::crmpushprojectadd',
                'CrmProject::projectlibadd',
                'CrmProject::projectliblist',
                'CrmProject::projectlibdetail',
                'CrmProject::projectlibedit',
                'CrmProject::projectlibdel',
                'CrmProject::newsadd',
                'CrmProject::newslist',
                'CrmProject::newsedit',
                'CrmProject::newsdel',
                'CrmProject::newsdetail',
                'CrmProject::companyanalyze',
                'CrmProject::monitorcommit',
                'CrmProject::indexnewslist',
                'CrmProject::indexnewsdetail',
                'CrmProject::indexprojectlist',
                'CrmProject::indexprojectdetail',
                'CrmProject::crmmenulist',
                'CrmProject::crmmenuselect',
                'CrmProject::crmmenuuser',
                'CrmProject::crmdptposition',
                'CrmProject::rolouserlist',
                'CrmProject::crmaddrolo',
                'CrmProject::crmuserdetail',
                'CrmProject::crmrolodetail',
                'CrmProject::crmdeluser',
                'CrmProject::crmdelrolo',
                'CrmProject::analyzelist',
                'CrmProject::insidemonitor',
                'Organization::dptadd',
                'Organization::dptedit',
                'Organization::dptdetail',
                'Organization::dptlist',
                'Organization::dptdel',
                'Organization::posadd',
                'Organization::poslist',
                'Organization::posdetail',
                'Organization::posedit',
                'Organization::posdel',
                'Test::test',
                'Test::test2',
            )
        );

        if (in_array($resource, $acl[$role])) {
            return true;
        }
        return false;
    }

    public function ajaxReturn($result)
    {
        return $result;
    }

}